import $store from '@/store/index.js';
import msgSqlite from "@/sqlite_services/msg.js"
//删除消息
const handle = async(data)=>{
	try{
		let inClause = data.msg_ids.map(v => `'${v}'`).join(',');
		let msgsql = await $store.state.user.sqlite.message.where([{
				field: 'msg_id',
				opt:'in',
				val: `(${inClause})`
			},
			{
				field: 'chat_type',
				val: `'${data.chat_type}'`,
				logic: 'AND'
			}
		]).get()
		if(msgsql.length){
			let msgIds = msgsql.map(message => message.msg_id);
			msgSqlite.delMsg(msgIds,data.chat_type)
			
			//通知页面删除
			//发送者的id
			let myid = $store.state.user.user_info.user_id
			let chat_id = data.sender_id == myid ? data.target_id : data.sender_id
			if(data.chat_type == 'group'){
				chat_id = data.target_id
			}
			
			//删除完了尝试更新会话
			$store.dispatch('delMsgSession', {
				chat_id: chat_id,
				msg_ids: data.msg_ids,
				chat_type: data.chat_type
			})
			
			const activeChatUuid = $store.state.chat.chatInfo && $store.state.chat.chatInfo.uuid || ''; // 访问根级状态的chatInfo
			
			if(chat_id == activeChatUuid){
				uni.$emit('onMessageControls', {
					type:'del_msg',
					data
				})
			}
			
		}
	}catch(e){
		console.log(e);
	}
}
export default handle